package com.jumi.microservice.stock.api.impl;

import com.google.common.collect.Lists;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.stock.api.IJmStockApi;
import com.jumi.microservice.stock.api.dto.CommodityMaterialStockDTO;
import com.jumi.microservice.stock.api.dto.CommodityStockReleaseDTO;
import com.jumi.microservice.stock.controller.StockCommodityController;
import com.jumi.microservice.stock.dto.StockMaterialStockDTO;
import com.jumi.microservice.stock.dto.commodity.CommodityApplyDTO;
import com.jumi.microservice.stock.service.CommodityApplyService;
import com.jumi.microservice.stock.service.StockService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 仓库库存对外提供服务
 * @ClassName StockApiServiceImpl
 * @Author Abel Yang
 * @Date 2020/8/27/027 9:20
 **/
@RestController
@Service
public class StockApiImpl implements IJmStockApi {

    @Resource
    private StockService stockService;

    @Resource
    private CommodityApplyService applyService;

    private static final Logger log = LoggerFactory.getLogger(StockCommodityController.class);

    /**
     * @Description: 商城查询物料库存列表
     * @Param materialIds
     * @Return {@link ResponseResult< List<  CommodityMaterialStockDTO >>}
     * @Throws
     * @Author Abel Yang
     * @Date 2020/8/29/029 10:11
     */
    @Override
    public ResponseResult<List<CommodityMaterialStockDTO>> getMaterialStockByMaterialIds(List<Long> materialIds) {
        List<CommodityMaterialStockDTO> result = Lists.newArrayList();

        List<StockMaterialStockDTO> stockList = stockService.getMaterialStock(materialIds, null);
        if (stockList.isEmpty()) {
            return ResponseResult.success(result);
        }
        stockList.forEach(stock -> {
            CommodityMaterialStockDTO commodityStock = new CommodityMaterialStockDTO();
            commodityStock.setMaterialId(stock.getMaterialId());
            commodityStock.setMaterialCode(stock.getMaterialCode());
            commodityStock.setAvailableStockNum(stock.getAvailableStockNum());
            result.add(commodityStock);
        });
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult addCommodityApply(CommodityApplyDTO applyRequest) {
        log.info("成功到添加了!");
        int rows = applyService.save(applyRequest);
        return ResponseResult.success("操作成功!", rows);
    }

    @Override
    public ResponseResult commodityStockRelease(List<CommodityStockReleaseDTO> commodityStockReleaseDTOS, String updateBy) {
        int rows = stockService.commodityStockRelease(commodityStockReleaseDTOS, updateBy);
        return ResponseResult.success("操作成功!", rows);
    }
}
